package com.houfukude.Activity;

import java.io.File;

import Utils.Audio_Recorder;
import Utils.File_Utils;
import Utils.V;
import com.houfukude.xtestmgr.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

public class RecordAudioActivity extends Activity {

	private ToggleButton record_btn;

	private Audio_Recorder audio_utils;
	protected boolean isSTOP = true;
	private static String path = null;
	private OnCheckedChangeListener listener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {

			if (isChecked) {
				Toast.makeText(getApplicationContext(), "正在录音...",
						Toast.LENGTH_SHORT).show();
				path = V.Audio_Directory +File_Utils.get_Audio_Name();
				audio_utils = new Audio_Recorder(path);
				audio_utils.startRecord();
				isSTOP = false;
			} else {
				audio_utils.stopRecord();
				Toast.makeText(getApplicationContext(),
						"另存为:" + path,
						Toast.LENGTH_SHORT).show();
				isSTOP = true;
				Intent data = new Intent();
				Uri uri = Uri.fromFile(new File(path));
				data.setData(uri);
				data.setFlags(V.id.NEW_AUDIO);
				setResult(RESULT_OK, data);
				finish();
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_audio_record);
		record_btn = (ToggleButton) findViewById(R.id.record_btn);
		record_btn.setOnCheckedChangeListener(listener);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (isSTOP) {
			super.onBackPressed();
		}
	}

}
